package org.idontknow.primegen;

import java.io.*;

/**
 * test class for byte array - file data transfer
 *
 * @author ZHAO Xinyu, Aug 4, 2006
 */

class ByteTable{
  
  public static void main(String[] args) {
    try{
      
      byte[] res = getBytesFromFile(System.getProperty("user.dir") + File.separator + "output" + File.separator + "prime_table");
      System.out.println(res.length);
      System.out.print(new String(res));
      System.out.println("\ntest successful!");

    }catch(IOException e){
      System.err.println("test failed!");
    }
  }

/**
 * getBytesFromFile method: reads data from a file into a byte array
 *
 * @param fileName the name of input file
 */

  public static byte[] getBytesFromFile(String fileName)
    throws IOException {

      byte[] bytes = null;

      try{
        File file = new File(fileName);
        InputStream is = new FileInputStream(file);

        long len = file.length();
        
        if(len > Integer.MAX_VALUE)
          System.out.println("File is too large!");
        else
          bytes = new byte[(int)len];

        int offset = 0;
        int sizeRead = 0;

        while(offset < bytes.length 
            && (sizeRead = is.read(bytes, offset, bytes.length-offset)) >= 0)
          offset += sizeRead;

//        String content = new String(bytes);

//        System.out.println(content);

      }catch(Exception e){
        System.err.println("error on opening input file!");
      }
      return bytes;
    }
}
